﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

///<summary>
///
///</summary>
///
public enum BalloonType
{
	right,
	left,
	down,
	up,
	one,
	two,
	three,
	four,
	five,
	six,
	seven,
}
public class BalloonFactory : MonoBehaviour
{
	static BalloonFactory instance;
	List<GameObject> balloonList = new List<GameObject>();
	Dictionary<string, GameObject> ballDict = new Dictionary<string, GameObject>();
	public GameObject balloonParent;
    BalloonType _curType;
	void Awake()
	{
		if (GameObject.FindGameObjectsWithTag("BalloonFactory").Length > 1)
		{
			Destroy(this.gameObject);
		}
		else
		{
			DontDestroyOnLoad(this.gameObject);
		}
	}
	public static BalloonFactory Instance
	{
		get
		{
			if (instance == null)
			{
				instance = GameObject.Find("BalloonFactory").GetComponent<BalloonFactory>();
			}
			return instance;
		}
	}

	public GameObject GetGameObject(string path)
	{
		GameObject obj = ChenckObjHideFromList();
		if (obj == null)
		{
			obj = InstantiateObj(path);
			balloonList.Add(obj);
		}
		else
			obj.SetActive(true);

        _curType = obj.GetComponent<BalloonInputKeyComponent>().curType;
		return obj;
	}
	public GameObject GetGameObject(string path, GameObject parent)
	{
		GameObject obj = ChenckObjHideFromList();
		if (obj == null)
		{
			obj = InstantiateObj(path);
			balloonList.Add(obj);
		}
		else
			obj.SetActive(true);
		return obj;
	}
	public GameObject GetGameObject(string path, GameObject parent, Vector3 pos)
	{
		GameObject obj = ChenckObjHideFromList();
		if (obj == null)
		{
			obj = InstantiateObj(path, pos);
			balloonList.Add(obj);
		}
		else
			obj.SetActive(true);
		obj.transform.SetParent(parent.transform);
		return obj;
	}

	public GameObject GetGameObject(string path, GameObject parent, Vector3 pos, BalloonType type)
	{
		GameObject obj = CheckObjHideFromDict();
        if (obj == null||ballDict.Count<11)
        {
            obj = InstantiateObj(path, pos);
            if (!ballDict.ContainsKey(type.ToString()))
                ballDict.Add(type.ToString(), obj);
        }
        else 
            obj.SetActive(true);
		obj.transform.position = pos;
		obj.transform.SetParent(parent.transform);
		return obj;
	}

	public GameObject GetParent()
	{
        if (balloonParent == null)
        {
            balloonParent = GameObject.Find("Canvas/balloonParent");
        }
		return balloonParent;
	}

	GameObject InstantiateObj(string path, Vector3 pos)
	{
		GameObject obj = GameObject.Instantiate(Resources.Load<GameObject>(path));
		obj.transform.position = pos;
		return obj;
	}
	GameObject InstantiateObj(string path)
	{
		GameObject obj = GameObject.Instantiate(Resources.Load<GameObject>(path));
		return obj;
	}

	GameObject CheckObjHideFromDict()
	{
        if (ballDict.Count < 1) return null;
        
		foreach (string str in ballDict.Keys)
		{
			if (!ballDict[str].activeInHierarchy)
			{
                if(inspectList(ballDict[str].GetComponent<BalloonInputKeyComponent>().curType))
				return ballDict[str];
			}
		}
        return null;
	}


	GameObject ChenckObjHideFromList()
	{
		foreach (GameObject obj in balloonList)
		{
			if (!obj.activeInHierarchy)
			{
				return obj;
			}
		}
		return null;
	}

    public void BalloonDontDestroy()
    {
        if (GameObject.FindGameObjectsWithTag("Balloon").Length > 0)
        {
            GameObject[] balloons = GameObject.FindGameObjectsWithTag("Balloon");
            for (int i = 0; i < balloons.Length; i++)
            {
                balloons[i].transform.SetParent(transform);
                balloons[i].SetActive(false);
            }
        }
        if (balloonParent != null)
        {
           foreach(Transform child in balloonParent.transform)
            {
                child.SetParent(transform);
                child.gameObject.SetActive(false);
            }
        }
        Debug.Log("气球转为不可销毁");
    }


	bool inspectList(BalloonType balloonType)
	{
		if (GameConfig.typeList.Count >= 11)
			GameConfig.typeList.Clear();
		foreach (BalloonType type in GameConfig.typeList)
		{
			if (balloonType == type || balloonType.ToString().Equals(type.ToString()))
			{
				return false;
			}
		}
		GameConfig.typeList.Add(balloonType);
		return true;
	}
	BalloonType RandomGetBalloonType()
	{
		BalloonType cur = (BalloonType)Random.Range(0, 11);
		while (true)
		{
			if (inspectList(cur))
			{
				break;
			}
			else
			{
				cur = (BalloonType)Random.Range(0, 11);
			}
		}
		return cur;
	}
}
